package com.latvia.managers;
  
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
  
import android.annotation.SuppressLint;
import android.content.Context; 
import android.graphics.Bitmap; 
import android.media.MediaScannerConnection; 
import android.net.Uri; 
import android.os.Environment; 
import android.util.Log; 
import android.widget.Toast; 
  
public class SaveForPicture { 
    private Context TheThis; 
    private String NameOfFolder = "/Cooking_App"; 
    private String NameOfFile   = "My_meal"; 
  
    public void SaveImage(Context context,Bitmap ImageToSave){ 
        TheThis = context; 
        //Sets strings for picture names
        String file_path = Environment.getExternalStorageDirectory().getAbsolutePath()+ NameOfFolder; 
        String CurrentDateAndTime= getCurrentDateAndTime(); 
        File dir = new File(file_path); 
          
        //Checks if the folder exists and if it doesnt then it creates one
        if(!dir.exists()){ 
            dir.mkdirs(); 
        } 
          
        File file = new File(dir, NameOfFile +CurrentDateAndTime+ ".jpg"); 
          
        try { 
        	//puts stuff in the file
            FileOutputStream fOut = new FileOutputStream(file); 
            ImageToSave.compress(Bitmap.CompressFormat.JPEG, 85, fOut); 
            fOut.flush(); 
            fOut.close(); 
            //makes the file scan the gallery to make the file visible
            MakeSureFileWasCreatedThenMakeAvabile(file); 
            AbleToSave(); 
              
        }  
        catch (FileNotFoundException e) {UnableToSave();} 
        catch (IOException e){UnableToSave();} 
          
          
           
    } 
  
      
  
    private void MakeSureFileWasCreatedThenMakeAvabile(File file) { 
        MediaScannerConnection.scanFile(TheThis, 
                new String[] { file.toString() }, null, 
                new MediaScannerConnection.OnScanCompletedListener() { 
            public void onScanCompleted(String path, Uri uri) { 
                Log.e("ExternalStorage", "Scanned " + path + ":"); 
                Log.e("ExternalStorage", "-> uri=" + uri); 
                 
            } 
        }); 
          
    } 
  
  
  
    @SuppressLint("SimpleDateFormat")
	private String getCurrentDateAndTime() { 
        Calendar c = Calendar.getInstance(); 
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); 
        String formattedDate = df.format(c.getTime()); 
        return formattedDate; 
    } 
      
      
private void UnableToSave() { 
    Toast.makeText(TheThis, "Picture cannot be saved to gallery", Toast.LENGTH_SHORT).show(); 
          
    } 

private void AbleToSave() { 
    Toast.makeText(TheThis, "Picture saved succesfully", Toast.LENGTH_SHORT).show(); 
          
    } 
} 